package com.lin.fengxiu.api.v1;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.lin.fengxiu.exception.http.NotFoundException;
import com.lin.fengxiu.model.Theme;
import com.lin.fengxiu.service.ThemeService;
import com.lin.fengxiu.vo.ThemePureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @ClassName ThemeController
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/3/13 11:59
 * @Version 1.0
 */
@RequestMapping("theme")
@RestController
@Validated
public class ThemeController {

    @Autowired
    private ThemeService themeService;

    @GetMapping("/by/names")
    public List<ThemePureVO> getThemeGroupByNames(@RequestParam(name = "names")String names){
        List<String> nameList = Arrays.asList(names.split(","));
        List<Theme> themes = themeService.findByNames(nameList);
        List<ThemePureVO> list = new ArrayList<>();
        themes.forEach(theme->{
//          封面的主体不需要Spu_id
            Mapper mapper = DozerBeanMapperBuilder.buildDefault();
//          使用 DozerBeanMapperBuilder 拷贝数据
            ThemePureVO vo = mapper.map(theme,ThemePureVO.class);
            list.add(vo);
        });
        return list;
    }

    @GetMapping("/name/{name}/with_spu")
    public Theme getThemeByNameWithSpu(@PathVariable(name = "name")String themeName){
        Optional<Theme> optionalTheme = this.themeService.findByName(themeName);
        return optionalTheme.orElseThrow(()-> new NotFoundException(30003));

    }
}
